#!/bin/sh
#
# $FreeBSD$
#
# PROVIDE: identd_stunnel
# REQUIRE: SERVERS
# KEYWORD: shutdown
#

. /etc/rc.subr

name=identd_stunnel
rcvar=identd_stunnel_enable
command=/usr/local/opnsense/scripts/stunnel/identd_stunnel.py
command_interpreter=/usr/local/bin/python3
pidfile="/var/run/${name}.pid"
load_rc_config $name

# Set defaults
: ${identd_stunnel_enable:=NO}

stop_cmd=identd_stunnel_stop

# kill configd
identd_stunnel_stop()
{
    if [ -z "$rc_pid" ]; then
      [ -n "$rc_fast" ] && return 0
      _run_rc_notrunning
      return 1
    fi

    echo -n "Stopping ${name}."
    # first ask gently to exit
    kill -15 ${rc_pid}

    # wait max 5 seconds for gentle exit
    for i in $(seq 1 50);
    do
        if [ -z "`/bin/ps -ax | /usr/bin/awk '{print $1;}' | /usr/bin/grep "^${rc_pid}"`" ]; then
            break
        fi
        sleep 0.1
    done

    # kill any remaining identd_stunnel processes (if still running)
    for identd_stunnel_pid in `/bin/ps -ax | grep 'identd_stunnel.py' | /usr/bin/awk '{print $1;}' `
    do
       kill -9 $identd_stunnel_pid >/dev/null 2>&1
    done

    echo  "..done"
}

run_rc_command $1
